package org.tea.inventory.admin.web.sys;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.admin.web.admin.BaseController;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.domain.sys.SysDictData;
import org.tea.inventory.service.ISysDictDataService;
import org.tea.inventory.service.ISysDictTypeService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典内层控制器")
@RestController
@RequestMapping("admin/system/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public Object list(SysDictData dictData,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        PageHelper.startPage(page, limit);
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        PageInfo<SysDictData> pageInfo = new PageInfo<>(list);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", pageInfo.getList());
        data.put("total", pageInfo.getTotal());
        return ResponseUtil.ok(data);
    }


    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/info")
    @ApiOperation(value = "查询字典数据详细")
    public Object getInfo(Long dictCode) {
        return ResponseUtil.ok(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    public Object dictType(String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return ResponseUtil.ok(data);
    }

    /**
     * 新增字典类型
     */
    @PostMapping("add")
    @ApiOperation(value = "新增字典类型")
    public Object add(@Validated @RequestBody SysDictData dict) {
        return ResponseUtil.ok(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PostMapping("edit")
    @ApiOperation(value = "修改保存字典类型")
    public Object edit(@Validated @RequestBody SysDictData dict) {
        return ResponseUtil.ok(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除字典类型")
    public Object remove(String dictCodes) {
        Long[] split = Convert.toLongArray(dictCodes.split(","));
        dictDataService.deleteDictDataByIds(split);
        return ResponseUtil.ok();
    }

}
